<?php
/**
 * 从供应商获取的商品信息数据操作
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/2 0002
 * Time: 下午 4:23
 */

namespace DataAPI\Model;
use Think\Model;

class SupplierProductsModel extends Model
{
    public $Errmessage = '';
    public $model;
    private $app_id = '';

    public function __construct($app_id = null)
    {
        $this->app_id = $app_id;
        $this->model = M('supplier_products');
    }

    /**
     * 返回错误信息
     * @return mixed
     */
    public function getError() {
        return $this->Errmessage;
    }
    /**
     * 增加商品信息
     * @param $product
     */
    public function addProducts($product){
        $product['app_id'] =$this->app_id;
        if($newProduct = $this->productsVerification($product)){
            if($this->getOneProduct($product['earn13'])){
                $result = $this->model->where(['earn13'=>$product['earn13']])->save($newProduct);
                if($result){
                    $this->Errmessage = '数据修改成功';
                    return $result;
                }else{
                    $this->Errmessage = '数据修改失败';
                    return false;
                }
            }else{
                $result = $this->model->add($product);
                if($result){
                    $this->Errmessage = '数据录入成功';
                    return $result;
                }else{
                    $this->Errmessage = '数据录入失败';
                    return false;
                }
            }
        }
    }

    /**
     * 商品字段验证
     */
    public function productsVerification($product){

        if(!$product){
            $this->Errmessage = '传入参数错误';
            return false;
        }
        if(!isset($product['earn13']) || empty($product['earn13'])){
            $this->Errmessage = '商品en13码不能为空';
            return false;
        }
        if(!isset($product['earn13']) || empty($product['earn13'])){
            $this->Errmessage = '商品en13码不能为空';
            return false;
        }

        if(!isset($product['name']) || empty($product['name'])){
            $this->Errmessage = '商品名称不能为空';
            return false;
        }
        if(!isset($product['weight']) || empty($product['weight'])){
            $this->Errmessage = '商品总量不能为空';
            return false;
        }
        if(!isset($product['market_price']) || empty($product['market_price'])){
            $this->Errmessage = '商品'.$product['earn13'].'市场价不能为空';
            return false;
        }
        if(!isset($product['acting_price']) || empty($product['acting_price'])){
            $this->Errmessage = '商品'.$product['earn13'].'一件代发价格不能为空';
            return false;
        }
        if(!isset($product['wholesale_price']) || empty($product['wholesale_price'])){
            $this->Errmessage = '商品'.$product['earn13'].'批发价不能为空';
            return false;
        }
        if(!isset($product['lock_price']) || empty($product['lock_price'])){
            $this->Errmessage = '商品'.$product['earn13'].'锁货价不能为空';
            return false;
        }
        if(!isset($product['wholesale_start_num']) || empty($product['wholesale_start_num'])){
            $this->Errmessage = '商品'.$product['earn13'].'最低批发数量不能为空';
            return false;
        }
        if(!isset($product['lock_start_num']) || empty($product['lock_start_num'])){
            $this->Errmessage = '商品'.$product['earn13'].'最低锁货数量不能为空';
            return false;
        }
        if(!isset($product['price']) || empty($product['price'])){
            $this->Errmessage = '商品'.$product['earn13'].'建议零售价不能为空';
            return false;
        }
        if(!isset($product['stock_num'])){
            $this->Errmessage = '商品'.$product['earn13'].'库存不能为空';
            return false;
        }
        $product['createdtime'] = time();
//        print_r($product);die;
        return $product;
    }

    /**
     * 分页获取商品数据
     * @param $page
     * @param $size
     * @param $where
     */
    public function getProducts($page=null,$size=null,$where){
        $count      = $this->model->where($where)->count();// 查询满足要求的总记录数

        $Page       = getpage($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数($size)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $this->model->where($where)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();

        $data['show'] = $show;
        $data['list'] = $list;
        return $data;
    }

    /**
     * 查询一个商品
     * @param $earn13
     * @return mixed
     */
    public function getOneProduct($earn13){
        $dataInfo = $this->model->where("earn13=$earn13")->select();
        return $dataInfo;
    }

    /**
     * 判断是否已经同步
     * @param $earn13
     * @return bool
     */
    public function _isSynchronization($earn13){
        $dataInfo = $this->getOneProduct($earn13);
        if($dataInfo['is_sync'] == 0){
            return false;
        }
        return true;
    }

    /**
     * 判断是否达到同步条件
     * @param $earn13
     * @return bool
     */
    public function _isSureSynchronization($earn13){
        $dataInfo = $this->getOneProduct($earn13);
        if($this->productsVerification($dataInfo)){
            return true;
        }
        return false;
    }
}